﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KD.Sms.DAL.SqlLite
{
    internal sealed partial class Main
    {
        /// <summary>
        /// 写系统日志
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="logContext"></param>
        public void LogWrite(SysLogType logType, string logContext)
        {
            _strSql = String.Format(@"INSERT INTO SysLog
                    (Log_Id, Log_Type, Log_Content)
                    VALUES(IFNULL((SELECT MAX(Log_Id) FROM SysLog), 0) + 1, 
                    {0}, '{1}')",
                Convert.ToInt32(logType), string.Format("用户：{0} 操作：{1}", CacheUser.Chinese_name, logContext));
            SQLiteHelper.ExecuteNonQuery(_strSql, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="btime"></param>
        /// <param name="etime"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public DataTable DoQuerySysLog(string btime, string etime, string content)
        {
            _strSql = @"select Log_id,strftime('%Y-%m-%d %H:%M:%S', Log_Time) as Log_Time,Log_Content from SysLog where 1 = 1 ";
            _strSql += string.Format(" and Log_Time >= '{0}'", btime);
            _strSql += string.Format(" and Log_Time <= '{0}'", etime);
            if (!string.IsNullOrEmpty(content))
                _strSql += string.Format(" and Log_Content like '%{0}%'", content);
            _strSql += " order by Log_id asc";
            return SQLiteHelper.GetDataTable(_strSql);
        }
    }
}
